package com.compass.doctor.ui.patient.fragment

import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.compass.doctor.base.BaseVBFragment
import com.compass.doctor.databinding.FragmentBaseListBinding
import com.compass.doctor.ui.patient.adapter.RecordInformationAdapter
import com.compass.doctor.ui.patient.viewmodel.PatientViewModel
import com.compass.framework.constant.KEY_PATIENT_ID
import com.compass.framework.decoration.NormalItemDecoration
import com.compass.framework.ext.gone
import com.compass.framework.ext.load
import com.compass.framework.ext.visible
import com.compass.framework.utils.dpToPx
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener

/**
 * Description：患者就诊记录
 * Date:2023/12/14 14:13
 */
class RecordInformationFragment : BaseVBFragment<FragmentBaseListBinding>(), OnLoadMoreListener,
    OnRefreshListener {

    private val patientViewModel: PatientViewModel by viewModels()
    private lateinit var mAdapter: RecordInformationAdapter
    private var mPage = 1
    private lateinit var patientId: String //患者id
    override fun initView(view: View, savedInstanceState: Bundle?) {
        arguments?.let {
            patientId = it.getString(KEY_PATIENT_ID)!!
        }
        initRecyclerView()
    }

    private fun initRecyclerView() {
        mBinding?.refreshLayout?.apply {
            setEnableRefresh(true)
            setEnableLoadMore(true)
            setOnRefreshListener(this@RecordInformationFragment)
            setOnLoadMoreListener(this@RecordInformationFragment)
        }
        mAdapter = RecordInformationAdapter()

        mBinding?.recyclerView?.apply {
            layoutManager = LinearLayoutManager(context)
            addItemDecoration(NormalItemDecoration().apply {
                setBounds(top = dpToPx(10), bottom = dpToPx(10))

                setLastBottom(false)
            })
            adapter = mAdapter
        }


        mAdapter.onItemClickListener = { _: View, position: Int ->
            val item = mAdapter.getItem(position)

        }

    }



    override fun initData() {
        getPrescribedList()
        patientViewModel.recordLiveData.observe(this){
            if (mPage == 1) {
                mAdapter.setData(it)
                mBinding?.refreshLayout?.finishRefresh()
                mBinding?.refreshLayout?.setEnableLoadMore(load(it?.size))
                if (it.isNullOrEmpty()) {
                    mBinding?.emptyView?.visible()
                } else {
                    mBinding?.emptyView?.gone()
                }
            } else {
                mBinding?.refreshLayout?.finishLoadMore()
                if (!it.isNullOrEmpty()) {
                    mAdapter.addAll(it)
                } else {
                    mBinding?.refreshLayout?.setEnableLoadMore(false)
                }
            }
        }
    }


    private fun getPrescribedList() {
        patientViewModel.getConsultationList(count = mPage, pageSize = 10, patientId)

    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        mPage++
        getPrescribedList()
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        mPage = 1
        getPrescribedList()
    }
}